package com.example.fileoperate.operate.impl;/*
 * @Auther:hantianbing
 * @Date:2023/7/27
 * @Description:
 */

import com.example.fileoperate.operate.OperateTwoFile;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName CompareTwoDependencyVersion
 * @Description
 * @Author hantianbing
 * @Date 2023/7/27 14:28
 **/
public class CompareTwoDependencyVersion implements OperateTwoFile {
    @Override
    public void opreateFileNoAffect(File file1, File file2) {

    }

    @Override
    public void opreateFilesNoAffect(List<File> files1, List<File> files2) {
        Map<String, String> jarVersion = new HashMap<>();
        Map<String, List<String>> ans = new HashMap<>();
        String rule1 = "-\\d";
        String rule2 = "\\.jar";
        Pattern pattern1 = Pattern.compile(rule1);
        Pattern pattern2 = Pattern.compile(rule2);


        for (File file : files1) {
            String fileName = file.getName();
            Matcher matcher1 = pattern1.matcher(fileName);

            if (!matcher1.find()){
                continue;
            }
            int start1 = matcher1.start();
            int end1 = matcher1.end();
            String key = fileName.substring(0, start1);

            Matcher matcher2 = pattern2.matcher(fileName);
            if (!matcher2.find()) {
                System.out.println("不是jar");
            }
            int start2 = matcher2.start();
            String version = fileName.substring(end1 - 1, start2);
            jarVersion.put(key, version);
        }

        for (File file : files2){
            String fileName = file.getName();
            Matcher matcher1 = pattern1.matcher(fileName);
            if (!matcher1.find()) {
                continue;
            }
            int start1 = matcher1.start();
            int end1 = matcher1.end();
            String key = fileName.substring(0, start1);

            Matcher matcher2 = pattern2.matcher(fileName);
            if (!matcher2.find()) {
                System.out.println("不是jar");
            }
            int start2 = matcher2.start();
            String version = fileName.substring(end1 - 1, start2);
            String s = jarVersion.get(key);
            if(StringUtils.hasText(s)){
                if(!s.equals(version)){
                    List<String> temp = new ArrayList<>();
                    temp.add(key +s);
                    temp.add(key + version);
                }
            }
        }

        Collection<List<String>> values = ans.values();
        for (List<String> value : values) {
            for (String s : value) {
                System.out.print(s +"   ");
            }
            System.out.println("=================");
        }
    }

    @Override
    public File operateFileHaveAffect(File file1, File file2) {
        return null;
    }


}
